﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BusinessLogicLayer;

namespace logicunivesityinventorystore.Store
{
    public partial class ViewRequest : System.Web.UI.Page
    {
        List<OrderDetails> orderDetails;
        PlaceOrder placeOrder;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
                orderDetails = new List<OrderDetails>();
            }
        }

        private void BindGridView()
        {

            gvViewRequest.DataSource = new RetrivalListFacade().GetRequest(Session["UserName"].ToString());
            gvViewRequest.DataBind();
            if (gvViewRequest.Rows.Count == 0)
            {
               // btnPrint.Visible = false;
                btnSelectAll.Visible = false;
                btnDeSelect.Visible = false;
                btnPurchaseOrder.Visible = false;
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('No Requitions available');window.location.href = 'HomePage_Store.aspx'", true);
            }
            else
            {
                foreach (GridViewRow row in gvViewRequest.Rows)
                {
                    if (Convert.ToInt32(row.Cells[5].Text) >= Convert.ToInt32(row.Cells[6].Text))
                    {

                        row.Cells[1].ForeColor = System.Drawing.Color.Red;
                        CheckBox select = (CheckBox)row.Cells[0].FindControl("chkItem");
                        select.Checked = true;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvViewRequest.Rows)
            {
                CheckBox select = (CheckBox)row.Cells[0].FindControl("chkItem");
                select.Checked = true;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvViewRequest.Rows)
            {
                CheckBox select = (CheckBox)row.Cells[0].FindControl("chkItem");
                select.Checked = false;
            }
        }

        protected void btnPurchaseOrder_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvViewRequest.Rows)
            {
                CheckBox select = (CheckBox)row.Cells[0].FindControl("chkItem");
                if (select.Checked)
                {
                    orderDetails = (List<OrderDetails>)Session["OrderDetails"];
                    string itemCode = (row.Cells[1]).Text;
                    int orderedQuantity = Convert.ToInt32(row.Cells[3].Text);
                    int reorderQuantity = Convert.ToInt32(row.Cells[5].Text);
                    placeOrder = new PlaceOrder();
                    OrderDetails ord = new OrderDetails();
                    ord.ItemCode = itemCode;
                    ord.QtyOrdered = orderedQuantity + reorderQuantity;
                    ord.Remark = row.Cells[2].Text;
                    orderDetails.Add(ord);
                    
                    Session["OrderDetails"] = orderDetails;
                    //Session["PurchaseOrder"] = PurchaseOrder;
                }
            }
            Response.Redirect("PurchaseStationary.aspx");

            
        }
    }
}   